package com.b.controller;

import com.b.utils.EsDataSyncUtil;
import com.b.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ES数据同步控制器
 */
@RestController
@RequestMapping("/es/sync")
@CrossOrigin
public class EsDataSyncController {

    @Resource
    private EsDataSyncUtil esDataSyncUtil;

    /**
     * 一键上传所有实体类数据到ES，无需传参
     * @return 操作结果
     */
    @PostMapping("/uploadAll")
    public Result uploadAll() {
        Result result = new Result();
        try {
            esDataSyncUtil.uploadAllEntitiesToEs();
            result.setCode("200").setMsg("上传成功");
        } catch (Exception e) {
            result.setCode("500").setMsg("上传失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 一键删除ES中所有实体类对应索引的数据，无需传参
     * @return 操作结果
     */
    @PostMapping("/deleteAll")
    public Result deleteAll() {
        Result result = new Result();
        try {
            esDataSyncUtil.deleteAllIndicesFromEs();
            result.setCode("200").setMsg("删除成功");
        } catch (Exception e) {
            result.setCode("500").setMsg("删除失败: " + e.getMessage());
        }
        return result;
    }
    /**
     * 一键删除ES中所有索引，无需传参
     * @return 操作结果
     */
    @PostMapping("/deleteAllIndices")
    public Result deleteAllIndices() {
        Result result = new Result();
        try {
            esDataSyncUtil.deleteAllEntitiesFromEs();
            result.setCode("200").setMsg("删除成功");
        } catch (Exception e) {
            result.setCode("500").setMsg("删除失败: " + e.getMessage());
        }
        return result;
    }
} 